﻿<Page x:Class="Probel.Ldoc.Views.Pages.RecordEditorView"
      
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:t="clr-namespace:Probel.Ldoc.Views.Properties"
      xmlns:dto="clr-namespace:Probel.Ldoc.ViewModels.Dto;assembly=Probel.Ldoc.ViewModels"
      xmlns:conv="clr-namespace:Probel.Ldoc.Views.Converters"
      xmlns:ctrl="clr-namespace:Probel.Ldoc.Views.Pages"
      xmlns:s="clr-namespace:Probel.Ldoc.Views.Helpers"
      
      mc:Ignorable="d" 
      d:DesignWidth="1200" 
      d:DesignHeight="768"
      Loaded="Page_Loaded">
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Themes/Generic.xaml"/>
                <ResourceDictionary>
                    <conv:StatusToImageConverter x:Key="statusToImage"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    <Page.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Save"
                        Executed="SaveCommand_Executed"
                        CanExecute="SaveCommand__CanExecute"/>
        
        <CommandBinding Command="ApplicationCommands.Delete"
                        Executed="RemoveRecord_Executed"/>
        
        <CommandBinding Command="ApplicationCommands.New"
                        Executed="AddRecord_Executed"
                        CanExecute="AddRecord_CanExecute"/>
    </Page.CommandBindings>
    <Page.InputBindings>
        <KeyBinding Command="{Binding RemoveRecordCommand}"
                    Key="Delete"/>
    </Page.InputBindings>
    <DockPanel LastChildFill="True">
        <StatusBar DockPanel.Dock="Bottom" Background="#FF465A7D">
            <StatusBarItem>
                <DockPanel>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding UiMessage, Converter={StaticResource statusToImage}}" 
                           Width="16" 
                           Height="16" 
                           Margin="0,0,5,0"/>
                        <TextBlock Text="{Binding UiMessage.Message}"/>
                    </StackPanel>
                </DockPanel>
            </StatusBarItem>
            <StatusBarItem HorizontalAlignment="Right">
                <TextBlock>
                    <Run Text="{x:Static t:Messages.Status_Patient}"/>
                    <Run Text="{Binding CurrentPatientName,Mode=OneWay}"/>
                </TextBlock>
            </StatusBarItem>
        </StatusBar>
        <HeaderedContentControl Header="{x:Static t:Messages.Header_RecordList}">
            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Top"
                            Orientation="Horizontal" HorizontalAlignment="Right"
                            >
                    <Button Style="{DynamicResource EditionButtonStyle}"
                            Click="Click_CreateRecord">
                        <Image Source="/Probel.Ldoc.Views;component/Images/File/New_File.png"/>
                    </Button>
                    <Button Style="{DynamicResource EditionButtonStyle}"
                            Click="Click_CreateFolder">
                        <Image Source="/Probel.Ldoc.Views;component/Images/File/New_Folder.png" />
                    </Button>
                </StackPanel>
                <TreeView x:Name="treeView" 
                          DockPanel.Dock="Bottom"
                          Width="200"
                          ItemsSource="{Binding RecordCabinet.Drawers}" 
                          SelectedItemChanged="TreeView_SelectedItemChanged" 
                          PreviewMouseRightButtonDown="TreeView_PreviewMouseRightButtonDown">
                    <TreeView.Resources>
                        <Style x:Key="ExpandingImageStyle" 
                               TargetType="{x:Type Image}">
                            <Setter Property="Source" Value="/Probel.Ldoc.Views;component/Images/File/Folder_Closed.png"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" 
                                             Value="True">
                                    <Setter Property="Source" Value="/Probel.Ldoc.Views;component/Images/File/Folder_Open.png"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>

                        <Style TargetType="TreeViewItem">
                            <Setter Property="IsExpanded" Value="True" />
                        </Style>

                        <Style TargetType="{x:Type TextBox}">
                            <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
                        </Style>

                        <DataTemplate x:Key="TitleTemplate">
                            <DockPanel Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ctrl:RecordEditorView}}}">
                                <Image DockPanel.Dock="Left" Source="/Probel.Ldoc.Views;component/Images/File/TextFile.png"/>
                                <TextBlock x:Name="textBlock" 
                                           Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}"/>
                                <TextBox x:Name="textBox" 
                                         Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" 
                                         Visibility="Collapsed" />
                                <DockPanel.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="{x:Static t:Messages.Btn_RenameFolder}"
                                                  Command="{Binding EditCommand}">
                                            <MenuItem.Icon>
                                                <Image Source="/Probel.Ldoc.Views;component/Images/File/Rename.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="{x:Static t:Messages.Btn_DeleteRecord}"
                                                  Command="ApplicationCommands.Delete">
                                            <MenuItem.Icon>
                                                <Image Source="/Probel.Ldoc.Views;component/Images/File/Delete.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                </DockPanel.ContextMenu>
                            </DockPanel>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsEditing}" Value="True">
                                    <Setter TargetName="textBlock" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="textBox" Property="Visibility" Value="Visible"/>
                                    <Setter TargetName="textBox" Property="FocusManager.FocusedElement"
                                            Value="{Binding ElementName=textBox}"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>

                        <HierarchicalDataTemplate DataType="{x:Type dto:RecordDrawerViewModel}" 
                                                  ItemsSource="{Binding Records}"
                                                  ItemTemplate="{StaticResource TitleTemplate}">

                            <!-- http://stackoverflow.com/questions/11202225/how-to-set-datacontext-for-treeviewitem-for-contextmenu -->
                            <DockPanel Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ctrl:RecordEditorView}}}">
                                <DockPanel.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="{x:Static t:Messages.Btn_AddDefaultRecord}"
                                                  Command="ApplicationCommands.New"
                                                  CommandParameter="{Binding Id}">
                                            <MenuItem.Icon>
                                                <Image Source="/Probel.Ldoc.Views;component/Images/File/New_File.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="{x:Static t:Messages.Btn_RenameFolder}"
                                                  Command="{Binding EditCommand}">
                                            <MenuItem.Icon>
                                                <Image Source="/Probel.Ldoc.Views;component/Images/File/Rename.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                </DockPanel.ContextMenu>
                                <Image DockPanel.Dock="Left"
                                       Style="{StaticResource ExpandingImageStyle}"/>
                                <TextBlock x:Name="textBlock" Text="{Binding Name}"/>
                                <TextBox x:Name="textBox" 
                                         Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" 
                                         Visibility="Collapsed"/>
                            </DockPanel>

                            <HierarchicalDataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsEditing}" Value="True">
                                    <Setter TargetName="textBlock" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="textBox" Property="Visibility" Value="Visible"/>
                                    <Setter TargetName="textBox" Property="FocusManager.FocusedElement"
                                            Value="{Binding ElementName=textBox}"/>
                                </DataTrigger>
                            </HierarchicalDataTemplate.Triggers>
                        </HierarchicalDataTemplate>
                    </TreeView.Resources>
                    <TreeView.InputBindings>
                        <KeyBinding Key="F2"
                                    Command="{Binding ActivateEditionCommand}"
                                    CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}"/>
                        <KeyBinding Key="Escape"
                                    Command="{Binding CancelEditionCommand}"
                                    CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}"/>
                        <KeyBinding Key="Enter"
                                    Command="{Binding RenameCommand}"
                                    CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}"/>
                    </TreeView.InputBindings>
                </TreeView>
            </DockPanel>
        </HeaderedContentControl>
        <HeaderedContentControl Header="{x:Static t:Messages.Header_Editor}">
            <TabControl>
                <TabItem>
                    <TabItem.Header>
                        <TextBlock>
                            <Run Text="Record: "/>
                            <Run Text="{Binding CurrentRecord.Name}" FontWeight="Bold"/>
                            <Run Text="["/>                            
                            <Run Text="{x:Static t:Messages.Tab_RecordCreated}"/>
                            <Run Text="{Binding CurrentRecord.Created}" FontWeight="Bold"/>
                            <Run Text="-"/>
                            <Run Text="{x:Static t:Messages.Tab_RecordUpdated}"/>
                            <Run Text="{Binding CurrentRecord.Updated}" FontWeight="Bold"/>
                            <Run Text="]"/>
                        </TextBlock>
                    </TabItem.Header>
                    <DockPanel LastChildFill="True">
                        <ToolBarTray DockPanel.Dock="Top">
                            <!-- History Actions -->
                            <ToolBar>
                                <Button Command="ApplicationCommands.Save"
                                ToolTip="{x:Static t:Messages.Action_Save}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Save.png"/>
                                </Button>
                                <Button Command="ApplicationCommands.Undo"                                           
                                ToolTip="{x:Static t:Messages.Action_Undo}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Arrow_Undo.png"/>
                                </Button>
                                <Button Command="ApplicationCommands.Redo" 
                                      ToolTip="{x:Static t:Messages.Action_Redo}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Arrow_Redo.png"/>
                                </Button>
                                <Separator/>
                                <Button Click="Click_RenameRecord" 
                                        ToolTip="{x:Static t:Messages.Btn_RenameFolder}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/File/Rename.png"/>
                                </Button>
                                <Button Command="ApplicationCommands.Delete"
                                        ToolTip="{x:Static t:Messages.Btn_RemoveRecord}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/File/Delete.png"/>
                                </Button>
                            </ToolBar>
                            <!-- Text layout -->
                            <ToolBar>
                                <Button Command="EditingCommands.ToggleBold"                                           
                                      ToolTip="{x:Static t:Messages.Action_Bold}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Bold.png"/>
                                </Button>
                                <Button Command="EditingCommands.ToggleItalic" 
                                      ToolTip="{x:Static t:Messages.Action_Italic}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Italic.png"/>
                                </Button>
                                <Button Command="EditingCommands.ToggleUnderline"
                                      ToolTip="{x:Static t:Messages.Action_Underline}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Underline.png"/>
                                </Button>
                                <Separator/>
                                <Button Command="EditingCommands.ToggleBullets"                                           
                                      ToolTip="{x:Static t:Messages.Action_Bullet}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Bullets.png"/>
                                </Button>
                                <Button Command="EditingCommands.ToggleNumbering" 
                                      ToolTip="{x:Static t:Messages.Action_Numbering}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Numbering.png"/>
                                </Button>
                                <Separator/>
                                <ToggleButton x:Name="highlightButton" 
                                      ToolTip="{x:Static t:Messages.Action_Highlight}"
                                Click="Click_HighlightText">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Highlight.png"/>
                                </ToggleButton>

                                <Separator/>
                                <Button Command="EditingCommands.AlignLeft"                                           
                                      ToolTip="{x:Static t:Messages.Action_AlignLeft}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Lines_Text_left.png"/>
                                </Button>
                                <Button Command="EditingCommands.AlignCenter" 
                                      ToolTip="{x:Static t:Messages.Action_AlignCenter}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Lines_Text_centered.png"/>
                                </Button>
                                <Button Command="EditingCommands.AlignRight" 
                                      ToolTip="{x:Static t:Messages.Action_AlignRight}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Lines_Text_right.png"/>
                                </Button>
                                <Button Command="EditingCommands.AlignJustify" 
                                      ToolTip="{x:Static t:Messages.Action_AlignJustify}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Lines_Text_justify.png"/>
                                </Button>
                            </ToolBar>
                            <!-- Clipboard actions -->
                            <ToolBar>
                                <Button Command="ApplicationCommands.Copy"                                             
                                ToolTip="{x:Static t:Messages.Action_Copy}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Copy.png"/>
                                </Button>
                                <Button Command="ApplicationCommands.Cut" 
                                ToolTip="{x:Static t:Messages.Action_Cut}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Cut.png"/>
                                </Button>
                                <Button Command="ApplicationCommands.Paste"
                                ToolTip="{x:Static t:Messages.Action_Paste}">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Paste.png"/>
                                </Button>
                            </ToolBar>
                            <!-- Fonts actions -->
                            <ToolBar>
                                <Label Content="Font:"/>
                                <ComboBox x:Name="cbFont" 
                                  Width="150"
                                  IsEditable="True" 
                                  SelectionChanged="cbFont_SelectionChanged"/>
                                <ComboBox x:Name="cbSize" 
                                  Width="50" 
                                  SelectionChanged="cbSize_SelectionChanged">
                                    <sys:Double>7</sys:Double>
                                    <sys:Double>8</sys:Double>
                                    <sys:Double>9</sys:Double>
                                    <sys:Double>10</sys:Double>
                                    <sys:Double>11</sys:Double>
                                    <sys:Double>12</sys:Double>
                                    <sys:Double>14</sys:Double>
                                    <sys:Double>16</sys:Double>
                                    <sys:Double>18</sys:Double>
                                    <sys:Double>20</sys:Double>
                                    <sys:Double>22</sys:Double>
                                    <sys:Double>24</sys:Double>
                                    <sys:Double>26</sys:Double>
                                    <sys:Double>28</sys:Double>
                                    <sys:Double>36</sys:Double>
                                    <sys:Double>48</sys:Double>
                                    <sys:Double>72</sys:Double>
                                </ComboBox>
                            </ToolBar>
                            <!-- Misc actions -->
                            <ToolBar>
                                <Button ToolTip="{x:Static t:Messages.Action_History}"
                                        Click="Click_ShowHistory">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/History.png"/>
                                </Button>
                                <Button ToolTip="{x:Static t:Messages.Action_Macro}"
                                        Click="Click_ShowMacroEditor">
                                    <Image Source="/Probel.Ldoc.Views;component/Images/Editing/Script.png"/>
                                </Button>
                            </ToolBar>
                        </ToolBarTray>
                        <RichTextBox x:Name="editor"
                                     Margin="5"
                                     VerticalScrollBarVisibility="Visible"
                                     PreviewMouseUp="Editor_MouseUp" 
                                     IsEnabled="False" 
                                     SelectionChanged="Editor_SelectionChanged"
                                     TextChanged="Editor_TextChanged"
                                     FontFamily="{s:SettingsBinding EditorFontFamily}" 
                                     FontSize="{s:SettingsBinding EditorFontSize}">
                            <RichTextBox.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Command="ApplicationCommands.Copy" 
                                      Header="{x:Static t:Messages.Action_Copy}" 
                                      InputGestureText="Ctrl+C">
                                        <MenuItem.Icon>
                                            <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Copy.png"/>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Command="ApplicationCommands.Cut" 
                                      Header="{x:Static t:Messages.Action_Cut}"
                                      InputGestureText="Ctrl+X">
                                        <MenuItem.Icon>
                                            <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Cut.png"/>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Command="ApplicationCommands.Paste" 
                                      Header="{x:Static t:Messages.Action_Paste}"
                                      InputGestureText="Ctrl+V">
                                        <MenuItem.Icon>
                                            <Image Source="/Probel.Ldoc.Views;component/Images/Clipboard/Paste.png"/>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <Separator x:Name="macrosSeparator"/>
                                    <MenuItem x:Name="macrosMenu" Header="Macros"/>
                                </ContextMenu>
                            </RichTextBox.ContextMenu>
                        </RichTextBox>
                    </DockPanel>
                </TabItem>
            </TabControl>
        </HeaderedContentControl>
    </DockPanel>
</Page>
